package com.example.nkj.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class checkbox extends AppCompatActivity
{
    //对控件对象进行声明
    private TextView textView;
    private CheckBox checkbox1;
    private CheckBox checkbox2;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);

        //通过控件的ID来得到代表控件的对象
        textView = (TextView) findViewById(R.id.textView);
        checkbox1 = (CheckBox) findViewById(R.id.cb1);
        checkbox2 = (CheckBox) findViewById(R.id.cb2);

        //为第一个 CheckBox 注册监听
        checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                //如果第一个 CheckBox 被选中
                if(isChecked == true)
                {
                    textView.setText("checkbox1");
                }
            }
        });

        //为第二个 CheckBox 注册监听
        checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
                //如果第二个 CheckBox 被选中
                if(isChecked == true)
                {
                    textView.setText("checkbox2");
                }
            }
        });
    }
}
